package com.retlife.tvshow.global.manager

import android.content.Context
import android.media.AudioAttributes
import android.media.AudioManager
import android.media.MediaPlayer
import android.media.SoundPool
import com.retlife.tvshow.R
import com.retlife.tvshow.global.MyApplication

class BgmManager(context: Context) {
    private val soundPool: SoundPool = SoundPool.Builder().setMaxStreams(1).setAudioAttributes(
        AudioAttributes.Builder().setLegacyStreamType(AudioManager.STREAM_MUSIC).build()
    ).build()
    private val mediaPlayer: MediaPlayer
    private val voiceCountDown: Int
//    private val bgm: Int
    private var countDownStream: Int = 0
//    private var bgmStream: Int = 0

    init {
        voiceCountDown = soundPool.load(context, R.raw.count_down, 1)
//        bgm = soundPool.load(context, R.raw.bgm, 1)
        mediaPlayer = MediaPlayer.create(context, R.raw.bgm)
        mediaPlayer.isLooping = true
        mediaPlayer.setVolume(0.5f,0.5f)
    }

    fun countDown() {
        countDownStream = soundPool.play(voiceCountDown, 1f, 1f, 1, 0, 1f)
    }

    fun playBgm() {
//        bgmStream = soundPool.play(bgm, 1f, 1f, 1, 0, 1f)

mediaPlayer.start()
    }

    fun stopBgm() {
//        soundPool.stop(bgmStream)
        mediaPlayer.stop()
        mediaPlayer.prepare()

    }

    fun release() {
        soundPool.stop(countDownStream)
        stopBgm()
        soundPool.unload(voiceCountDown)
        mediaPlayer.release()
    }

    fun restart(){
        mediaPlayer.seekTo(0)
        mediaPlayer.start()
    }
    fun pause() {
        mediaPlayer.pause()
    }

    fun resume() {
        playBgm()
    }

    companion object {
        var instance: BgmManager? = null

    }
}
